<?php
/**
 * 简单日志工具
 *
 * @author wangshen
 */
class Tool_Log
{

    /**
     * debug
     */
    public static function debug($key , $exinfo)
    {
        self::addFileLog("debug" , $key."\t".$exinfo, true);
    }

    private static function addFileLog($filename, $desc, $micro=false)
    {
        $fp = fopen(MULTILOG_PATH.$filename, 'a');
        if ($fp)
        {
            $time = date('Y-m-d H:i:s');
            if ($micro)
            {
                $time .= "\t" . self::getMicro();
            }
            $flog = sprintf("%s\t%s\t%s\t%s\n",
                    $time, self::getIp(), self::getPid(), $desc);
            fwrite($fp, $flog);
            fclose($fp);
        }
    }

    private static function getPid()
    {
        static $pid = NULL;
        if (NULL === $pid)
        {
            $pid = getmypid().'.'.rand(0, 100000);
        }
        return $pid;
    }

    private static function getIp()
    {
        static $ip = NULL;
        if (NULL === $ip)
        {
            $ip = Tool_Ip::getClientIP();
        }
        return $ip;
    }

    private static function getMicro()
    {
        list($msec, $sec) = explode(" ", microtime());
        return ((float)$msec + (float)$sec);
    }
}
?>